package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbAnzhiMapper;
import com.sinog.biz.mapper.havexml.AreaCodeFullMapper;
import com.sinog.biz.mapper.havexml.PlacementBaseInfoMapper;
import com.sinog.biz.service.business.PlacementBaseInfoService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.core.domain.AbAnzhiResultVo;
import com.sinog.core.domain.PlacementBaseInfo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 安帮基地信息管理 - 安帮基地信息管理 - PlacementBaseInfoServiceImpl
 * @author dangyf
 * @date 2020年2月14日 20点48分
 */
@Service
public class PlacementBaseInfoServiceImpl extends ServiceImpl<PlacementBaseInfoMapper,PlacementBaseInfo> implements PlacementBaseInfoService {

    @Resource
    private PlacementBaseInfoMapper placementBaseInfoMapper;
    @Resource
    private AreaCodeFullMapper areaCodeFullMapper;
    @Resource
    private AbAnzhiMapper abAnzhiMapper;

    /**
     * 安帮工作资源管理 - 安帮基地信息管理 - 安帮基地信息管理 - 列表
     * @param searchKey searchKey
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param searchPlacement searchPlacement
     * @param departId departId
     * @return String
     */
    @Override
    public String getPlacementBaseInfoList(String searchKey,Integer pageIndex,Integer pageSize,String searchPlacement,String departId) {
        //参数转换
        searchKey = StrUtil.converseToNull(searchKey);
        searchPlacement = StrUtil.converseToNull(searchPlacement);
        //分页查询
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        String localCode;
        // 山西个性化需求:将本省单位的localCode放入列表,用于前台展示启用/禁用按钮
        {
            localCode = CodeUrlUtils.getLocalCode();
        }
        List<Map<String,String>> placementBaseInfoList = placementBaseInfoMapper.getPlacementBaseInfoList(page,searchKey,searchPlacement,departId,localCode);
        for(Map<String,String> map : placementBaseInfoList) {
            if(map.get("dh") != null) {
                Map<String,String> telAndPhone = PhoneUtil.splitTelAndPhone(map.get("dh"));
                map.put("dh",telAndPhone.get("phone"));
                map.put("tel",telAndPhone.get("tel"));
            }
        }
        page = page.setRecords(placementBaseInfoList);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安帮工作资源管理 - 安帮基地信息管理 - 安帮基地信息管理 - （修改/新增）
     * @param data data
     * @param loginUserParam loginUserParam
     * @return Result
     */
    @Override
    public Result save(String data,Map<String,String> loginUserParam) {
        //参数实体
        PlacementBaseInfo placementBaseInfo = JsonUtil.toBean(data,PlacementBaseInfo.class);
        Map<String, Object> paramMap = JsonUtil.toMap(data);
        Date date = new Date();
        // 住址省、市、县、街道名称
        String provinceCode = placementBaseInfo.getProvince();
        String cityCode = placementBaseInfo.getCity();
        String countyCode = placementBaseInfo.getCounty();
        String villageCode = placementBaseInfo.getVillage();
        placementBaseInfo.setBffwCode(Constants.NumberStr.NUM_STR_FU_ONE.equals(placementBaseInfo.getBffwCode()) ? null : placementBaseInfo.getBffwCode());
        if (StringUtils.isNotBlank(provinceCode)){
            Map<String,String> areaNames = areaCodeFullMapper.getAreaNames(provinceCode,cityCode,countyCode,villageCode);
            if (areaNames == null){
                return Result.failure(ReturnCode.SAVE_FAILED);
            }
            placementBaseInfo.setProvinceName(areaNames.get("shengName"));
            placementBaseInfo.setCityName(areaNames.get("dishiName"));
            placementBaseInfo.setCountyName(areaNames.get("quxianName"));
            placementBaseInfo.setVillageName(areaNames.get("jiedaoName"));
            if (StringUtils.isBlank(cityCode)){
                placementBaseInfo.setCityName("");
            }
            if (StringUtils.isBlank(countyCode)){
                placementBaseInfo.setCountyName("");
            }
            if (StringUtils.isBlank(villageCode)){
                placementBaseInfo.setVillageName("");
            }
        } else {
            placementBaseInfo.setProvinceName("");
            placementBaseInfo.setCityName("");
            placementBaseInfo.setCountyName("");
            placementBaseInfo.setVillageName("");
        }
        // 查找数据库,判断是否新增或修改
        if(Constants.Number.NUM_ZERO.equals(paramMap.get("flag"))) {
            placementBaseInfo.setClab(loginUserParam.get("departId"));
            placementBaseInfo.setClabName(loginUserParam.get("departName"));
            placementBaseInfo.setCreateUser(loginUserParam.get("loginUser"));
            placementBaseInfo.setCreateTime(date);
            String localCode = CodeUrlUtils.getLocalCode();
            if(Constants.XzqhCode.SHAN1XI.equals(localCode)) {
                placementBaseInfo.setShowStatus(Constants.NumberStr.NUM_STR_TWO);
            } else {
                placementBaseInfo.setShowStatus(Constants.NumberStr.NUM_STR_ONE);
            }
        }
        //更新
        placementBaseInfo.setUpdateUser(loginUserParam.get("loginUser"));
        placementBaseInfo.setUpdateTime(date);
        //入库
        boolean insertResult = placementBaseInfo.insertOrUpdate();
        if(insertResult) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮基地信息管理 - 安帮基地信息管理 - 删除
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteOne(String id) {
        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
        boolean result = placementBaseInfo.deleteById(id);
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮基地信息管理 - 安帮基地信息管理 - 导出
     * @param departId departId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectPlaceBaseForExport(String departId) {
        return placementBaseInfoMapper.selectPlaceBaseForExport(departId);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 新增基地人数
     * @param data data
     * @return Result
     */
    @Override
    public Result saveJdNumByJdmc(String data) {
        Map<String, Object> map = JsonUtil.toMap(data);
        if(placementBaseInfoMapper.saveJdNumByJdmc(map.get("jdrs"),map.get("id")) > 0){
            return Result.success();
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 查找基地下是否有安置的服刑人员
     * @param id id
     * @return Result
     */
    @Override
    public Result getBaseHasPm(String id) {
        return Result.success(abAnzhiMapper.selectCount(new QueryWrapper<AbAnzhiResultVo>().eq("jdid", id)));
    }

    /**
     * 安帮工作资源管理 - 安帮基地信息管理 - 修改安帮基地信息的展示状态
     * @param id id
     * @param showStatus showStatus
     * @return Result
     */
    @Override
    public Result updateShows(String id,String showStatus) {
        PlacementBaseInfo placementBaseInfo = new PlacementBaseInfo();
        placementBaseInfo.setId(id);
        //若现在为展示状态
        if(Constants.NumberStr.NUM_STR_ONE.equals(showStatus)){
            placementBaseInfo.setShowStatus(Constants.NumberStr.NUM_STR_ONE);
        }else{
            //若现在为禁用状态
            placementBaseInfo.setShowStatus(Constants.NumberStr.NUM_STR_TWO);
        }
        int flag = placementBaseInfoMapper.updateById(placementBaseInfo);
        if(flag > 0 ){
            return Result.success();
        }
        return Result.failure(ReturnCode.UPDATE_FAILED);
    }
}